<?php
if ($detail == "") {
    $prize = Frontend::findPrize();
} else {
    $flag = false;
    $prize_arr = explode("-", $detail);
    $prize_id = end($prize_arr);
    $prize = Frontend::findPrizeById((int) $prize_id);
    if (isset($_SESSION["user_id"])) {
        $user = Frontend::findUserById((int) $_SESSION["user_id"]);
        foreach ($prize as $_prize) {
            foreach ($user as $_user) {
                if ($_user["gold"] - $_prize["value"] <= 0) {
                    $flag = true;
                }
            }
        }
    }
}
?>
<?php if ($detail == "") { ?>
    <div class="span8">
        <ul class="thumbnails award-thumbnails">
            <?php
            foreach ($prize as $_prize) {
                $str = trim(Core::str_process($_prize["name"]));
                $link = strtolower(preg_replace('/[^a-z0-9]+/i', '-', $str));
                ?>
                <li class="span4">
                    <div class="thumbnail">
                        <a href="<?php echo DOMAIN . "giaithuong/" . $link . "-" . $_prize["_id"]; ?>">
                            <img src="<?php echo $_prize["image"] ?>">
                        </a>
                        <h3><?php echo $_prize["name"] ?></h3>
                        <p>Số lượng: <span class="label label-important"><?php echo $_prize["quantity"] ?></span></p>
                        <a class="btn btn-large btn-inverse" href="<?php echo DOMAIN . "giaithuong/" . $link . "-" . $_prize["_id"]; ?>">
                            <?php echo $_prize["value"] ?> coins                          
                        </a>
                    </div>
                </li>
            <?php } ?>
        </ul>
    </div>
    <div class="span4">
        <div class="block">
            <legend style="margin-bottom: 5px;">Thành viên nhận giải</legend>
        </div>
    </div>
<?php } else { ?>
    <div class="span12">
        <?php foreach ($prize as $_prize) { ?>
            <div class="span4">
                <img src="<?php echo $_prize["image"] ?>" class="img-polaroid">
                <legend style="margin: 10px 0;"><?php echo $_prize["name"] ?></legend>

            </div>
            <div class="span4">
                <legend style="margin-bottom: 5px;"><?php echo $_prize["value"] ?> coins</legend>
                <div id="error_message" style="margin-bottom: 10px; display: none" class="alert"></div>
                <?php if (isset($_SESSION["user_id"]) && !$flag) { ?>
                    <a id="trade" class="btn btn-warning confirm" href="javascript:void(0)" onclick="trade('<?php echo $_SESSION["user_id"]; ?>', '<?php echo $_prize["_id"]; ?>')">Đổi sản phẩm</a>
                <?php } else { ?>
                    <?php if (!isset($_SESSION["user_id"])) { ?>
                        <div style="margin-bottom: 10px;" class="alert">
                            Bạn phải <a href="<?php echo DOMAIN ?>login"><b>đăng nhập</b></a> để đổi giải thưởng này. 
                        </div>
                    <?php } if ($flag) { ?>
                        <div style="margin-bottom: 10px;" class="alert">
                            Số coins trong tài khoản của bạn không đủ để đổi sản phẩm này. 
                        </div>
                    <?php } ?>
                <?php } ?>
            </div>
            <div class="span12"><p><?php echo $_prize["description"] ?></p></div>
                <?php } ?>
    </div>

<?php } ?>
<script>
            function trade($user_id, $prize_id) {
                $.ajax({
                    // The link we are accessing.
                    url: "/ajax.php",
                    // The type of request.
                    type: "get",
                    data: "act=trade&user_id=" + $user_id + "&prize_id=" + $prize_id,
                    // The type of data that is getting returned.
                    dataType: "html",
                    error: function() {
                    },
                    success: function(strData) {
                        // Load the content in to the page.
                        if (strData == 0) {
                            $("#error_message").html("Sản phẩm hiện tại đã hết hàng.");
                            $('#error_message').css({display: "block"});
                        } else {
                            $("#error_message").html(strData);
                            $('#error_message').css({display: "block"});
                        }
                    }
                });
            }
</script>

